<?php
/*
 * 订单接口
 * Created time: 2017-01-12 0012 16:57
 *
 * Author: junxxx
 */

require '../framework/bootstrap.inc.php';
$_W['uniacid'] = 17;
$uniacid = '17';

global $_W;
global $_GPC;
$operation = (!empty($_GPC['op']) ? $_GPC['op'] : 'get');
$type = $_GPC['type'];


$totals = array();
$r_type = array('退款', '退货退款', '换货');
$paytype = array(
    0  => array('css' => 'default', 'name' => '未支付'),
    1  => array('css' => 'danger', 'name' => '余额支付'),
    11 => array('css' => 'default', 'name' => '后台付款'),
    2  => array('css' => 'danger', 'name' => '在线支付'),
    21 => array('css' => 'success', 'name' => '微信支付'),
    22 => array('css' => 'warning', 'name' => '支付宝支付'),
    23 => array('css' => 'warning', 'name' => '银联支付'),
    25 => array('css' => 'primary', 'name' => '易宝支付'),
    26 => array('css' => 'primary', 'name' => '易宝网银支付'),
    27 => array('css' => 'success', 'name' => 'App微信支付'),
    28 => array('css' => 'warning', 'name' => 'App支付宝支付'),
    29 => array('css' => 'paypal', 'name' => 'paypal支付'),
    3  => array('css' => 'primary', 'name' => '货到付款'),
    4  => array('css' => 'primary', 'name' => '到店支付')
);
$orderstatus = array(
    -1 => array('css' => 'default', 'name' => '已关闭'),
    0  => array('css' => 'danger', 'name' => '待付款'),
    1  => array('css' => 'info', 'name' => '待发货'),
    2  => array('css' => 'warning', 'name' => '待收货'),
    3  => array('css' => 'success', 'name' => '已完成')
);



$store_list = m('order')->getStoreList();

if ($operation == 'get') {

    $pindex = max(1, intval($_GPC['page']));
    $psize = SZ_YI_PSIZE;
    $status = ($_GPC['status'] == '' ? 1 : $_GPC['status']);
    $sendtype = (!isset($_GPC['sendtype']) ? 0 : $_GPC['sendtype']);
    $condition = ' AND o.uniacid = :uniacid and o.deleted=0';
    $join_table = '';
    $paras = array(':uniacid' => $_W['uniacid']);
    $starttime = $pstarttime = $fstarttime = $sstarttime = strtotime('-1 month');
    $endtime = $pendtime = $fendtime = $sendtime = time();

    if (!empty($_GPC['supplier_uid'])) {
        $condition .= ' AND o.supplier_uid = :supplier_uid ';
        $paras[':supplier_uid'] = $_GPC['supplier_uid'];
    }

    if (!empty($_GPC['time'])) {
        $starttime = strtotime($_GPC['time']['start']);
        $endtime = strtotime($_GPC['time']['end']);

        if ($_GPC['searchtime'] == '1') {
            $condition .= ' AND o.createtime >= :starttime AND o.createtime <= :endtime ';
            $paras[':starttime'] = $starttime;
            $paras[':endtime'] = $endtime;
        }
    }

    if (!empty($_GPC['ptime'])) {
        $pstarttime = strtotime($_GPC['ptime']['start']);
        $pendtime = strtotime($_GPC['ptime']['end']);

        if ($_GPC['psearchtime'] == '1') {
            $condition .= ' AND o.paytime >= :pstarttime AND o.paytime <= :pendtime ';
            $paras[':pstarttime'] = $pstarttime;
            $paras[':pendtime'] = $pendtime;
        }
    }

    if (!empty($_GPC['ftime'])) {
        $fstarttime = strtotime($_GPC['ftime']['start']);
        $fendtime = strtotime($_GPC['ftime']['end']);

        if ($_GPC['fsearchtime'] == '1') {
            $condition .= ' AND o.finishtime >= :fstarttime AND o.finishtime <= :fendtime ';
            $paras[':fstarttime'] = $fstarttime;
            $paras[':fendtime'] = $fendtime;
        }
    }

    if (!empty($_GPC['stime'])) {
        $sstarttime = strtotime($_GPC['stime']['start']);
        $sendtime = strtotime($_GPC['stime']['end']);

        if ($_GPC['ssearchtime'] == '1') {
            $condition .= ' AND o.sendtime >= :sstarttime AND o.sendtime <= :sendtime ';
            $paras[':sstarttime'] = $sstarttime;
            $paras[':sendtime'] = $sendtime;
        }
    }

    if ($_GPC['paytype'] != '') {
        if ($_GPC['paytype'] == '2') {
            $condition .= ' AND ( o.paytype =21 or o.paytype=22 or o.paytype=23 )';
        }
        else {
            $condition .= ' AND o.paytype =' . intval($_GPC['paytype']);
        }
    }

    if (empty($_W['isagent'])) {
        if ($_GPC['cancel'] == 1) {
            $orderids = pdo_fetchall('select orderid from ' . tablename('sz_yi_cancel_goods') . ' where uniacid=' . $_W['uniacid'] . ' ');
            $ids = '';

            foreach ($orderids as $key => $value) {
                if ($key != 0) {
                    $ids .= ',' . $value['orderid'];
                }
                else {
                    $ids .= $value['orderid'];
                }
            }

            if (!empty($orderids)) {
                $condition .= ' and o.id in (' . $ids . ') ';
            }
        }
    }

    if (!empty($_GPC['good_name']) || !empty($_GPC['good_id'])) {
        $good_condition = (!empty($_GPC['good_name']) ? 'g.title LIKE \'%' . $_GPC['good_name'] . '%\'' : 'g.id = \'' . $_GPC['good_id'] . '\'');
        $conditionsp_goods = pdo_fetchall('select og.orderid from ' . tablename('sz_yi_order_goods') . ' og left join ' . tablename('sz_yi_goods') . ' g on (g.id=og.goodsid) where og.uniacid=' . $_W['uniacid'] . ' and ' . $good_condition . ' group by og.orderid ');
        $conditionsp_goodsid = '';

        foreach ($conditionsp_goods as $value) {
            $conditionsp_goodsid .= '\'' . $value['orderid'] . '\', ';
        }

        if (!empty($conditionsp_goodsid)) {
            $condition .= ' AND o.id in (' . substr($conditionsp_goodsid, 0, -2) . ') ';
        }
        else {
            $condition .= ' AND o.id = \'\' ';
        }
    }

    if (!empty($_GPC['keyword'])) {
        $_GPC['keyword'] = trim($_GPC['keyword']);
        $condition .= ' AND (o.ordersn LIKE \'%' . $_GPC['keyword'] . '%\' OR o.pay_ordersn LIKE \'%' . $_GPC['keyword'] . '%\' OR o.ordersn_general LIKE \'%' . $_GPC['keyword'] . '%\')';
    }

    if (!empty($_GPC['expresssn'])) {
        $_GPC['expresssn'] = trim($_GPC['expresssn']);
        $condition .= ' AND o.expresssn LIKE \'%' . $_GPC['expresssn'] . '%\'';
    }

    if (!empty($_GPC['member'])) {
        $_GPC['member'] = trim($_GPC['member']);
        $sql = 'SELECT m.openid FROM ' . tablename('sz_yi_member') . " m WHERE (m.realname LIKE :member \n                OR m.mobile LIKE :member OR m.nickname LIKE :member) AND m.uniacid = :uniacid \n                UNION \n                SELECT a.openid FROM " . tablename('sz_yi_member_address') . " a \n                WHERE (a.realname LIKE :member OR a.mobile LIKE :member) AND a.uniacid = :uniacid";
        $member_paras = array(':uniacid' => $_W['uniacid'], ':member' => '%' . $_GPC['member'] . '%');
        $members = pdo_fetchall($sql, $member_paras);
        $openids = '';

        foreach ($members as $value) {
            $openids .= '\'' . $value['openid'] . '\', ';
        }

        if (!empty($openids)) {
            $condition .= ' AND o.openid in (' . substr($openids, 0, -2) . ') ';
        }
        else {
            $condition .= ' AND o.openid = \'\' ';
        }
    }

    if (!empty($_GPC['saler'])) {
        $_GPC['saler'] = trim($_GPC['saler']);
        $condition .= ' AND (sm.realname LIKE \'%' . $_GPC['saler'] . '%\' or sm.mobile LIKE \'%' . $_GPC['saler'] . '%\' or sm.nickname LIKE \'%' . $_GPC['saler'] . '%\' ' . ' or s.salername LIKE \'%' . $_GPC['saler'] . '%\' )';
        $join_table .= ' left join ' . tablename('sz_yi_member') . ' sm on sm.openid = o.verifyopenid and sm.uniacid=o.uniacid left join ' . tablename('sz_yi_saler') . ' s on s.openid = o.verifyopenid and s.uniacid=o.uniacid ';
    }

    if (!empty($_GPC['storeid'])) {
        $_GPC['storeid'] = trim($_GPC['storeid']);
        $condition .= ' AND o.verifystoreid=' . intval($_GPC['storeid']);
    }

    if (!empty($_GPC['csid'])) {
        $_GPC['csid'] = trim($_GPC['csid']);
        $condition .= ' AND o.cashierid=' . intval($_GPC['csid']);
    }

    if (p('hotel')) {
        if ($type == 'hotel') {
            $condition .= ' AND o.order_type=3';
        }
        else {
            $condition .= ' AND o.order_type<>3';
        }
    }
    else {
        $condition .= ' AND o.order_type<>3';
    }

    $statuscondition = '';

    if ($status != 'all') {
        if ($status == -1) {
            ca('order.view.status_1');
        }
        else {
            ca('order.view.status' . intval($status));
        }

        switch ($status) {
            case '-1':
                $statuscondition = ' AND o.status=-1 and o.refundtime=0';
                break;

            case '4':
                $statuscondition = ' AND o.refundtime=0 AND o.refundid<>0 AND r.status>=0 AND r.status!=2';
                break;

            case '5':
                $statuscondition = ' AND o.refundtime<>0';
                break;

            case '1':
                $statuscondition = ' AND ( o.status = 1 or (o.status=0 and o.paytype=3) )';
                break;

            case '0':
                $statuscondition = ' AND o.status = 0 and o.paytype<>3';
                break;

            default:
                $statuscondition = ' AND o.status = ' . intval($status);
                break;
        }
    }

    $bonusagentid = intval($_GPC['bonusagentid']);

    if (!empty($bonusagentid)) {
        $sql = 'select distinct orderid from ' . tablename('sz_yi_bonus_goods') . ' where mid=' . $bonusagentid . ' ORDER BY id DESC';
        $bonusoderids = pdo_fetchall($sql);
        $inorderids = '';

        if (!empty($bonusoderids)) {
            foreach ($bonusoderids as $key => $value) {
                if ($key != 0) {
                    $inorderids .= ',';
                }

                $inorderids = $value['orderid'];
            }

            $condition .= ' and  o.id in(' . $inorderids . ')';
        }
        else {
            $condition .= ' and  o.id=0';
        }
    }

    $agentid = intval($_GPC['agentid']);
    $plugin_commission = p('commission');
    $level = 0;

    if ($plugin_commission) {
        $cset = $plugin_commission->getSet();
        $level = intval($cset['level']);
    }

    $olevel = intval($_GPC['olevel']);
    if (!empty($agentid) && (0 < $level)) {
        $agent = $plugin_commission->getInfo($agentid, array());

        if (!empty($agent)) {
            $agentLevel = $plugin_commission->getLevel($agentid);
        }

        if (empty($olevel)) {
            if (1 <= $level) {
                $condition .= ' and  ( o.agentid=' . intval($_GPC['agentid']);
            }

            if ((2 <= $level) && (0 < $agent['level2'])) {
                $condition .= ' or o.agentid in( ' . implode(',', array_keys($agent['level1_agentids'])) . ')';
            }

            if ((3 <= $level) && (0 < $agent['level3'])) {
                $condition .= ' or o.agentid in( ' . implode(',', array_keys($agent['level2_agentids'])) . ')';
            }

            if (1 <= $level) {
                $condition .= ')';
            }
        }
        else if ($olevel == 1) {
            $condition .= ' and  o.agentid=' . intval($_GPC['agentid']);
        }
        else if ($olevel == 2) {
            if (0 < $agent['level2']) {
                $condition .= ' and o.agentid in( ' . implode(',', array_keys($agent['level1_agentids'])) . ')';
            }
            else {
                $condition .= ' and o.agentid in( 0 )';
            }
        }
        else {
            if ($olevel == 3) {
                if (0 < $agent['level3']) {
                    $condition .= ' and o.agentid in( ' . implode(',', array_keys($agent['level2_agentids'])) . ')';
                }
                else {
                    $condition .= ' and o.agentid in( 0 )';
                }
            }
        }
    }

    $cond = '';

    if (p('supplier')) {
        if ($perm_role == 1) {
            $cond .= ' and o.supplier_uid=' . $_W['uid'] . ' ';
        }
    }

    if ($_W['ispost']) {
        $result = pdo_fetch('SELECT COUNT(distinct o.ordersn_general) as total, ifnull(sum(o.price),0) as totalmoney FROM ' . tablename('sz_yi_order') . " AS o \n            LEFT JOIN " . tablename('sz_yi_order_refund') . ' r ON r.id =o.refundid ' . $join_table . ' WHERE 1 ' . $condition . ' ' . $statuscondition . ' ' . $cond, $paras);
        $total = $result['total'];
        $totalmoney = $result['totalmoney'];
        $pager = pagination($total, $pindex, $psize);
        show_json(1, array('pager' => $pager, 'total' => $total, 'totalmoney' => floatval($totalmoney)));
    }

    if (p('supplier')) {
        if ($cond) {
            $supplierapply = pdo_fetchall('select a.id,u.uid,p.realname,p.mobile,p.banknumber,p.accountname,p.accountbank,a.applysn,a.apply_money,a.apply_time,a.type,a.finish_time,a.status from ' . tablename('sz_yi_supplier_apply') . ' a ' . ' left join' . tablename('sz_yi_perm_user') . ' p on p.uid=a.uid ' . 'left join' . tablename('users') . ' u on a.uid=u.uid where u.uid=' . $_W['uid']);
            $totals['status9'] = count($supplierapply);
            $supplier_info = p('supplier')->getSupplierInfo($_W['uid']);
            $costmoney = $supplier_info['costmoney'];
            $openid = pdo_fetchcolumn('select openid from ' . tablename('sz_yi_perm_user') . ' where uid=:uid and uniacid=:uniacid', array(':uid' => $_W['uid'], ':uniacid' => $_W['uniacid']));

            if (empty($openid)) {
                message('暂未绑定微信，请联系管理员', '', 'error');
            }

            $applytype = intval($_GPC['applytype']);
            $apply_ordergoods_ids = '';

            foreach ($supplier_info['sp_goods'] as $key => $value) {
                if ($key == 0) {
                    $apply_ordergoods_ids .= $value['ogid'];
                }
                else {
                    $apply_ordergoods_ids .= ',' . $value['ogid'];
                }
            }

            if (!empty($applytype)) {
                $applysn = m('common')->createNO('commission_apply', 'applyno', 'CA');
                $data = array('uid' => $_W['uid'], 'apply_money' => $costmoney, 'apply_time' => time(), 'status' => 0, 'type' => $applytype, 'applysn' => $applysn, 'uniacid' => $_W['uniacid'], 'apply_ordergoods_ids' => $apply_ordergoods_ids);
                pdo_insert('sz_yi_supplier_apply', $data);
                @file_put_contents(IA_ROOT . '/addons/sz_yi/data/apply.log', print_r($data, 1), FILE_APPEND);

                if (pdo_insertid()) {
                    foreach ($supplier_info['sp_goods'] as $ids) {
                        $arr = array('supplier_apply_status' => 2);
                        pdo_update('sz_yi_order_goods', $arr, array('id' => $ids['ogid']));
                    }

                    $tmp_sp_goods = $supplier_info['sp_goods'];
                    $tmp_sp_goods['applyno'] = $applysn;
                    @file_put_contents(IA_ROOT . '/addons/sz_yi/data/sp_goods.log', print_r($tmp_sp_goods, 1), FILE_APPEND);
                }

                message('提现申请已提交，请耐心等待!', $this->createWebUrl('order/list'), 'success');
            }
        }
    }

    $sql = "SELECT count(1) AS suppliers_num, o.*, r.rtype \n            FROM " . tablename('sz_yi_order') . " AS o \n            LEFT JOIN " . tablename('sz_yi_order_refund') . ' r ON r.id =o.refundid ' . $join_table . ' WHERE 1 ' . $condition . ' ' . $statuscondition . ' ' . $cond . " GROUP BY o.ordersn_general ORDER BY o.createtime DESC,o.status DESC\n            LIMIT " . (($pindex - 1) * $psize) . ',' . $psize;
    $list = pdo_fetchall($sql, $paras);
    unset($value);
    $member_openids = array();
    $member_addresids = array();

    foreach ($list as $value) {
        $member_openids[] = '\'' . $value['openid'] . '\'';

        if (!empty($value['addressid'])) {
            $member_addresids[] = $value['addressid'];
        }
    }

    if (!empty($member_openids)) {
        $member_openids = implode(',', $member_openids);
        $member_addresids = implode(',', $member_addresids);
        $member_condition = (!empty($member_addresids) ? ' and a.id in (' . $member_addresids . ')' : '');
        $sql = "select m.openid, a.realname as arealname,a.mobile as amobile,a.province as aprovince,\n            a.city as acity,a.area as aarea,a.street as astreet,a.address as aaddress,m.nickname,m.id as mid,\n            m.realname as mrealname,m.mobile as mmobile \n            from " . tablename('sz_yi_member') . ' m' . "\n            left join " . tablename('sz_yi_member_address') . ' a on m.openid=a.openid and m.uniacid = a.uniacid ' . $member_condition . " \n            where m.openid in (" . $member_openids . ')  AND m.uniacid=:uniacid';
        $member_paras = array(':uniacid' => $_W['uniacid']);
        $members = pdo_fetchall($sql, $member_paras);

        if (!empty($members)) {
            foreach ($members as $member) {
                $order_members[$member['openid']] = $member;
            }

            unset($members);
        }
    }

    foreach ($list as &$value) {
        if (isset($order_members[$value['openid']])) {
            $value = $value + $order_members[$value['openid']];
        }

        if (p('supplier')) {
            $suppliers_num = $value['suppliers_num'];

            if (1 < $suppliers_num) {
                $value['vendor'] = '多供应商';
                $value['ischangePrice'] = 0;
            }
            else {
                if ($value['supplier_uid'] == 0) {
                    $value['vendor'] = '总店';
                }
                else {
                    $sup_username = p('supplier')->getSupplierName($value['supplier_uid']);
                    $value['vendor'] = '供应商：' . $sup_username;
                }

                $value['ischangePrice'] = 1;
            }
        }

        if ((1 < $suppliers_num) && ($value['status'] == 0)) {
            $order_all = pdo_fetchall('select * from ' . tablename('sz_yi_order') . ' where ordersn_general=:ordersn_general and uniacid=:uniacid', array(':ordersn_general' => $value['ordersn_general'], ':uniacid' => $_W['uniacid']));
            $orderids = array();
            $value['goodsprice'] = 0;
            $value['olddispatchprice'] = 0;
            $value['discountprice'] = 0;
            $value['deductprice'] = 0;
            $value['deductcredit2'] = 0;
            $value['deductenough'] = 0;
            $value['changeprice'] = 0;
            $value['changedispatchprice'] = 0;
            $value['couponprice'] = 0;
            $value['price'] = 0;

            foreach ($order_all as $k => $v) {
                $orderids[] = $v['id'];
                $value['goodsprice'] += $v['goodsprice'];
                $value['olddispatchprice'] += $v['olddispatchprice'];
                $value['discountprice'] += $v['discountprice'];
                $value['deductprice'] += $v['deductprice'];
                $value['deductcredit2'] += $v['deductcredit2'];
                $value['deductenough'] += $v['deductenough'];
                $value['changeprice'] += $v['changeprice'];
                $value['changedispatchprice'] += $v['changedispatchprice'];
                $value['couponprice'] += $v['couponprice'];
                $value['price'] += $v['price'];
            }

            $value['ordersn'] = $value['ordersn_general'];
            $orderid_where_in = implode(',', $orderids);
            $order_where = 'og.orderid in (' . $orderid_where_in . ')';
        }
        else {
            $order_where = 'og.orderid = ' . $value['id'];
        }

        if (!empty($value['cgid'])) {
            $value['isempty'] = 1;
        }
        else {
            $value['isempty'] = 0;
        }

        $order_status = $value['status'];
        $order_paytype = $value['paytype'];
        $value['statusvalue'] = $order_status;
        $value['statuscss'] = $orderstatus[$value['status']]['css'];
        $value['status'] = $orderstatus[$value['status']]['name'];
        if (($order_paytype == 3) && empty($value['statusvalue'])) {
            $value['statuscss'] = $orderstatus[1]['css'];
            $value['status'] = $orderstatus[1]['name'];
        }

        if ($order_status == 1) {
            if ($value['isverify'] == 1) {
                $value['status'] = '待使用';
            }
            else {
                if (empty($value['addressid'])) {
                    $value['status'] = '待取货';
                }
            }
        }

        if ($order_status == -1) {
            $value['status'] = $value['rstatus'];

            if (!empty($value['refundtime'])) {
                $value['status'] = '已退款';
            }
            else {
                $value['status'] = '已关闭';
            }
        }

        $value['paytypevalue'] = $order_paytype;
        $value['css'] = $paytype[$order_paytype]['css'];
        $value['paytype'] = $paytype[$order_paytype]['name'];

        if (empty($value['dispatchname'])) {
            $value['dispatchname'] = '快递配送';
        }

        if ($value['isverify'] == 1) {
            $value['dispatchname'] = '配送核销';
        }
        else if ($value['isvirtual'] == 1) {
            $value['dispatchname'] = '虚拟物品';
        }
        else if (!empty($value['virtual'])) {
            $value['dispatchname'] = '虚拟物品(卡密)<br/>自动发货';
        }
        else {
            if ($value['cashier'] == 1) {
                $value['dispatchname'] = '收银台支付';
            }
        }

        $value['dispatchname'] = empty($value['addressid']) ? '上门自提' : $value['dispatchname'];
        if (p('cashier') && ($value['cashier'] == 1)) {
            $value['name'] = set_medias(array('name' => $value['csname'], 'thumb' => $value['csthumb']), 'thumb');
        }

        if ((($value['dispatchtype'] == 1) && !empty($value['isverify'])) || !empty($value['virtual']) || !empty($value['isvirtual']) || ($value['cashier'] == 1)) {
            $value['address'] = '';
            $carrier = iunserializer($value['carrier']);

            if (is_array($carrier)) {
                $value['addressdata']['realname'] = $value['realname'] = $carrier['carrier_realname'];
                $value['addressdata']['mobile'] = $value['mobile'] = $carrier['carrier_mobile'];
                $value['addressdata']['address'] = $value['address'] = $carrier['address'];
            }
        }
        else {
            $address = iunserializer($value['address']);
            $isarray = is_array($address);
            $value['realname'] = $isarray ? $address['realname'] : $value['arealname'];
            $value['mobile'] = $isarray ? $address['mobile'] : $value['amobile'];
            $value['province'] = $isarray ? $address['province'] : $value['aprovince'];
            $value['city'] = $isarray ? $address['city'] : $value['acity'];
            $value['area'] = $isarray ? $address['area'] : $value['aarea'];

            if ($trade['is_street'] == '1') {
                $value['street'] = $isarray ? $address['street'] : $value['astreet'];
            }

            $value['address'] = $isarray ? $address['address'] : $value['aaddress'];
            $value['address_province'] = $value['province'];
            $value['address_city'] = $value['city'];
            $value['address_area'] = $value['area'];
            $value['address_street'] = $value['street'];
            $value['address_address'] = $value['address'];
            if (!empty($value['street']) && ($trade['is_street'] == '1')) {
                $value['address'] = $value['province'] . ' ' . $value['city'] . ' ' . $value['area'] . ' ' . $value['street'] . ' ' . $value['address'];
            }
            else {
                $value['address'] = $value['province'] . ' ' . $value['city'] . ' ' . $value['area'] . ' ' . $value['address'];
            }

            $value['addressdata'] = array('realname' => $value['realname'], 'mobile' => $value['mobile'], 'address' => $value['address']);
        }

        $commission1 = -1;
        $commission2 = -1;
        $commission3 = -1;
        $m1 = false;
        $m2 = false;
        $m3 = false;
        if (!empty($level) && empty($agentid)) {
            if (!empty($value['agentid'])) {
                $m1 = m('member')->getMember($value['agentid']);
                $commission1 = 0;

                if (!empty($m1['agentid'])) {
                    $m2 = m('member')->getMember($m1['agentid']);
                    $commission2 = 0;

                    if (!empty($m2['agentid'])) {
                        $m3 = m('member')->getMember($m2['agentid']);
                        $commission3 = 0;
                    }
                }
            }
        }

        $order_goods = pdo_fetchall("select g.id,g.title,g.thumb,g.goodssn,og.goodssn as option_goodssn, g.productsn,og.productsn as option_productsn, \n                        og.total,og.price,og.optionname as optiontitle, og.realprice,og.changeprice,og.oldprice,og.commission1,og.commission2,\n                        og.commission3,og.commissions,og.diyformdata,og.diyformfields \n                        from " . tablename('sz_yi_order_goods') . ' og ' . " \n                        left join " . tablename('sz_yi_goods') . ' g on g.id=og.goodsid ' . " \n                        where og.uniacid=:uniacid and " . $order_where, array(':uniacid' => $_W['uniacid']));
        $goods = '';

        foreach ($order_goods as &$og) {
            if (!empty($level) && empty($agentid)) {
                $commissions = iunserializer($og['commissions']);

                if (!empty($m1)) {
                    if (is_array($commissions)) {
                        $commission1 += (isset($commissions['level1']) ? floatval($commissions['level1']) : 0);
                    }
                    else {
                        $c1 = iunserializer($og['commission1']);
                        $l1 = $plugin_commission->getLevel($m1['openid']);
                        $commission1 += (isset($c1['level' . $l1['id']]) ? $c1['level' . $l1['id']] : $c1['default']);
                    }
                }

                if (!empty($m2)) {
                    if (is_array($commissions)) {
                        $commission2 += (isset($commissions['level2']) ? floatval($commissions['level2']) : 0);
                    }
                    else {
                        $c2 = iunserializer($og['commission2']);
                        $l2 = $plugin_commission->getLevel($m2['openid']);
                        $commission2 += (isset($c2['level' . $l2['id']]) ? $c2['level' . $l2['id']] : $c2['default']);
                    }
                }

                if (!empty($m3)) {
                    if (is_array($commissions)) {
                        $commission3 += (isset($commissions['level3']) ? floatval($commissions['level3']) : 0);
                    }
                    else {
                        $c3 = iunserializer($og['commission3']);
                        $l3 = $plugin_commission->getLevel($m3['openid']);
                        $commission3 += (isset($c3['level' . $l3['id']]) ? $c3['level' . $l3['id']] : $c3['default']);
                    }
                }
            }

            $goods .= '' . $og['title'] . '';

            if (!empty($og['optiontitle'])) {
                $goods .= ' 规格: ' . $og['optiontitle'];
            }

            if (!empty($og['option_goodssn'])) {
                $og['goodssn'] = $og['option_goodssn'];
            }

            if (!empty($og['option_productsn'])) {
                $og['productsn'] = $og['option_productsn'];
            }

            if (!empty($og['goodssn'])) {
                $goods .= ' 商品编号: ' . $og['goodssn'];
            }

            if (!empty($og['productsn'])) {
                $goods .= ' 商品条码: ' . $og['productsn'];
            }

            $goods .= ' 单价: ' . ($og['price'] / $og['total']) . ' 折扣后: ' . ($og['realprice'] / $og['total']) . ' 数量: ' . $og['total'] . ' 总价: ' . $og['price'] . ' 折扣后: ' . $og['realprice'] . '';
            if ($plugin_diyform && !empty($og['diyformfields']) && !empty($og['diyformdata'])) {
                $diyformdata_array = $plugin_diyform->getDatas(iunserializer($og['diyformfields']), iunserializer($og['diyformdata']));
                $diyformdata = '';

                foreach ($diyformdata_array as $da) {
                    $diyformdata .= $da['name'] . ': ' . $da['value'] . '';
                }

                $og['goods_diyformdata'] = $diyformdata;
            }
        }

        unset($og);
        if (!empty($level) && empty($agentid)) {
            $value['commission1'] = $commission1;
            $value['commission2'] = $commission2;
            $value['commission3'] = $commission3;
        }

        if (p('bonus')) {
            $bonus_area_money = pdo_fetchcolumn('select sum(money) from ' . tablename('sz_yi_bonus_goods') . ' where orderid=:orderid and uniacid=:uniacid and bonus_area!=0', array(':orderid' => $value['id'], ':uniacid' => $_W['uniacid']));
            $bonus_range_money = pdo_fetchcolumn('select sum(money) from ' . tablename('sz_yi_bonus_goods') . ' where orderid=:orderid and uniacid=:uniacid and bonus_area=0', array(':orderid' => $value['id'], ':uniacid' => $_W['uniacid']));
            if ((0 < $bonus_area_money) && (0 < $bonus_range_money)) {
                $bonus_money_all = $bonus_area_money + $bonus_range_money;
                $value['bonus_money_all'] = floatval($bonus_money_all);
            }

            $value['bonus_area_money'] = floatval($bonus_area_money);
            $value['bonus_range_money'] = floatval($bonus_range_money);
        }

        $value['goods'] = set_medias($order_goods, 'thumb');
        $value['goods_str'] = $goods;
    }

    unset($value);
    $condition = ' uniacid=:uniacid and deleted=0';
    if (p('hotel') && ($type == 'hotel')) {
        $condition .= ' and order_type=3';
        $join_order_type = ' and o.order_type=3';
    }
    else {
        $condition .= ' and order_type<>3';
        $join_order_type = ' and o.order_type<>3';
    }

    if (!empty($agentid) && (0 < $level)) {
        if (empty($olevel)) {
            if (1 <= $level) {
                $condition .= ' and  ( agentid=' . intval($_GPC['agentid']);
            }

            if ((2 <= $level) && (0 < $agent['level2'])) {
                $condition .= ' or agentid in( ' . implode(',', array_keys($agent['level1_agentids'])) . ')';
            }

            if ((3 <= $level) && (0 < $agent['level3'])) {
                $condition .= ' or agentid in( ' . implode(',', array_keys($agent['level2_agentids'])) . ')';
            }

            if (1 <= $level) {
                $condition .= ')';
            }
        }
        else if ($olevel == 1) {
            $condition .= ' and agentid=' . intval($_GPC['agentid']);
        }
        else if ($olevel == 2) {
            if (0 < $agent['level2']) {
                $condition .= ' and agentid in( ' . implode(',', array_keys($agent['level1_agentids'])) . ')';
            }
            else {
                $condition .= ' and agentid in( 0 )';
            }
        }
        else {
            if ($olevel == 3) {
                if (0 < $agent['level3']) {
                    $condition .= ' and agentid in( ' . implode(',', array_keys($agent['level2_agentids'])) . ')';
                }
                else {
                    $condition .= ' and agentid in( 0 )';
                }
            }
        }
    }

    $paras = array(':uniacid' => $_W['uniacid']);
    $totals = array();
    $supplier_cond = '';
    $supplier_conds = '';

    if (p('supplier')) {
        if (!empty($perm_role)) {
            $supplier_cond = ' AND supplier_uid=' . $_W['uid'];
            $supplier_conds = ' AND o.supplier_uid=' . $_W['uid'];
        }
    }

    $totals['all'] = pdo_fetchcolumn('SELECT COUNT(1) FROM ' . tablename('sz_yi_order') . '' . ' WHERE ' . $condition . $supplier_cond, $paras);
    $totals['status_1'] = pdo_fetchcolumn('SELECT COUNT(1) FROM ' . tablename('sz_yi_order') . '' . ' WHERE ' . $condition . ' and status=-1 and refundtime=0' . $supplier_cond, $paras);
    $totals['status0'] = pdo_fetchcolumn('SELECT COUNT(1) FROM ' . tablename('sz_yi_order') . '' . ' WHERE ' . $condition . ' and status=0 and paytype<>3' . $supplier_cond, $paras);
    $totals['status1'] = pdo_fetchcolumn('SELECT COUNT(1) FROM ' . tablename('sz_yi_order') . '' . ' WHERE ' . $condition . ' and ( status=1 or ( status=0 and paytype=3) )' . $supplier_cond, $paras);
    $totals['status2'] = pdo_fetchcolumn('SELECT COUNT(1) FROM ' . tablename('sz_yi_order') . '' . ' WHERE ' . $condition . ' and status=2' . $supplier_cond, $paras);
    $totals['status3'] = pdo_fetchcolumn('SELECT COUNT(1) FROM ' . tablename('sz_yi_order') . '' . ' WHERE ' . $condition . ' and status=3' . $supplier_cond, $paras);
    $totals['status4'] = pdo_fetchcolumn('SELECT COUNT(DISTINCT o.id) FROM ' . tablename('sz_yi_order') . ' o ' . ' left join ' . tablename('sz_yi_order_refund') . ' r on r.orderid= o.id' . ' left join ' . tablename('sz_yi_member') . ' m on m.openid=o.openid  and m.uniacid =  o.uniacid' . ' left join ' . tablename('sz_yi_member_address') . ' a on o.addressid = a.id ' . ' left join ' . tablename('sz_yi_member') . ' sm on sm.openid = o.verifyopenid and sm.uniacid=o.uniacid' . ' left join ' . tablename('sz_yi_saler') . ' s on s.openid = o.verifyopenid and s.uniacid=o.uniacid' . ' WHERE o.uniacid=:uniacid AND o.refundtime=0 AND o.refundid<>0 and r.status=0  and o.refundstate>=0 and o.deleted=0 AND r.status>=0 AND r.status!=2 ' . $supplier_conds . $join_order_type, array(':uniacid' => $_W['uniacid']));
    $totals['status5'] = pdo_fetchcolumn('SELECT COUNT(1) FROM ' . tablename('sz_yi_order') . '' . ' WHERE ' . $condition . ' and refundtime<>0' . $supplier_cond, $paras);
    if (p('hotel') && ($type == 'hotel')) {
        $totals['status6'] = pdo_fetchcolumn('SELECT COUNT(1) FROM ' . tablename('sz_yi_order') . '' . ' WHERE ' . $condition . ' and status=6', $paras);
    }

    $stores = pdo_fetchall('select id,storename from ' . tablename('sz_yi_store') . ' where uniacid=:uniacid ', array(':uniacid' => $_W['uniacid']));

    if (p('cashier')) {
        $cashier_stores = pdo_fetchall('select id,name from ' . tablename('sz_yi_cashier_store') . ' where uniacid=:uniacid ', array(':uniacid' => $_W['uniacid']));
    }

    $mt = mt_rand(5, 20);


    load()->func('tpl');

    if (p('hotel')) {
        if ($type == 'hotel') {
            include $this->template('web/order/list_hotel');
        }
        else {
            include $this->template('web/order/list');
        }
    }
    else {
        include $this->template('web/order/list');
    }

    exit();
    return 1;